﻿

namespace QFlow.Windows.Forms
{
    using System;
    using System.Linq;
    using QFlow.Data;
    using System.Windows.Forms;

    public partial class NewVendor : QFlow.Utils.BaseForm
    {
        bool _New;
        int _VendorId;
        public NewVendor(bool dialog, QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            _New = true;
            if (dialog)
                btnCancel.Visible = true;
        }

        public NewVendor(int vendorId, QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
            _VendorId = vendorId;
            Vendor vendor = this.Context.Vendors.SingleOrDefault(v => v.VendorID == _VendorId);
            txtName.Text = vendor.Name;
            txtRemarks.Text = vendor.Remarks;
            txtPerson.Text = vendor.ContactPerson;
            txtAddress.Text = vendor.Address;
            txtPhone.Text = vendor.Phone;
            txtEmail.Text = vendor.Email;
            _New = false;
            btnCancel.Visible = true;
            this.Text = QFlow.Windows.Properties.Resources.FRMEditVendor;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (superValidator1.Validate())
            {
                try
                {
                    if (_New)
                    {
                        Vendor vendor = new Vendor();
                        vendor.Name = txtName.Text;
                        if (!string.IsNullOrEmpty(txtRemarks.Text))
                            vendor.Remarks = txtRemarks.Text;
                        if (!string.IsNullOrEmpty(txtPerson.Text))
                            vendor.ContactPerson = txtPerson.Text;
                        if (!string.IsNullOrEmpty(txtAddress.Text))
                            vendor.Address = txtAddress.Text;
                        if (!string.IsNullOrEmpty(txtPhone.Text))
                            vendor.Phone = txtPhone.Text;
                        if (!string.IsNullOrEmpty(txtEmail.Text))
                            vendor.Email = txtEmail.Text;
                        vendor.Accounts.Add(new Account());

                        this.Context.Vendors.InsertOnSubmit(vendor);
                        this.Context.SubmitChanges();
                    }
                    else
                    {
                        Vendor vendor = this.Context.Vendors.SingleOrDefault(v => v.VendorID == _VendorId);
                        vendor.Name = txtName.Text;
                        if (!string.IsNullOrEmpty(txtRemarks.Text))
                            vendor.Remarks = txtRemarks.Text;
                        if (!string.IsNullOrEmpty(txtPerson.Text))
                            vendor.ContactPerson = txtPerson.Text;
                        if (!string.IsNullOrEmpty(txtAddress.Text))
                            vendor.Address = txtAddress.Text;
                        if (!string.IsNullOrEmpty(txtPhone.Text))
                            vendor.Phone = txtPhone.Text;
                        if (!string.IsNullOrEmpty(txtEmail.Text))
                            vendor.Email = txtEmail.Text;                       
                        this.Context.SubmitChanges();
                    }
                    PostSave();
                }
                catch
                {
                    MessageBox.Show(QFlow.Windows.Properties.Resources.MBCantSaveChanges, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
        }

        private void PostSave()
        {
            this.Close();
        }

        private void NewVendor_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            if (txtName.CausesValidation)
            {
                txtName.CausesValidation = false;
                txtPhone.CausesValidation = false;
                txtEmail.CausesValidation = false;
                this.Close();
            }
        }
    }
}
